package com.coder.bike.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coder.bike.entity.Payment;
import com.coder.bike.mapper.PaymentMapper;
import com.coder.bike.service.IPaymentService;
import com.coder.bike.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    // 你的微信收款码URL
    private static final String WECHAT_QR_CODE_URL = "https://sharing-bike.oss-cn-beijing.aliyuncs.com/c33e8a6ab2e0bc30c7e276eaf5cc0e5.jpg";

    @Override
    @Transactional
    public Result  createPayment(Integer customerId, BigDecimal amount, String subject) {
        // 生成支付单号
        String paymentNo = "PAY" + System.currentTimeMillis() + RandomUtil.randomNumbers(4);

        Payment payment = new Payment()
                .setPaymentNo(paymentNo)
                .setCustomerId(customerId)
                .setAmount(amount)
                .setSubject(subject)
                .setStatus("WAITING")
                .setQrcodeUrl(WECHAT_QR_CODE_URL)
                .setCreateTime(LocalDateTime.now())
                .setExpireTime(LocalDateTime.now().plus(30, ChronoUnit.MINUTES));

        if (save(payment)) {
            return Result.success(payment);
        }
        return Result.fail().setMessage("创建支付订单失败");
    }

    @Override
    public Result  queryPayment(String paymentNo) {
        Payment payment = lambdaQuery()
                .eq(Payment::getPaymentNo, paymentNo)
                .one();

        if (payment == null) {
            return Result.fail().setMessage("支付订单不存在");
        }

        return Result.success(payment);
    }

    @Override
    @Transactional
    public Result  mockPaymentSuccess(String paymentNo) {
        Payment payment = lambdaQuery()
                .eq(Payment::getPaymentNo, paymentNo)
                .one();

        if (payment == null) {
            return Result.fail().setMessage("支付订单不存在");
        }

        if (!"WAITING".equals(payment.getStatus())) {
            return Result.fail().setMessage("订单状态已变更，无法完成支付");
        }

        // 更新支付状态
        boolean updated = lambdaUpdate()
                .eq(Payment::getPaymentNo, paymentNo)
                .eq(Payment::getStatus, "WAITING")
                .set(Payment::getStatus, "SUCCESS")
                .set(Payment::getPayTime, LocalDateTime.now())
                .update();

        if (updated) {
            // 这里可以添加支付成功后的业务逻辑，比如更新用户余额等
            return Result.success(true);
        }

        return Result.fail().setMessage("支付状态更新失败");
    }
    @Override
    public Result  getPaymentRecords(Page<Payment> page, Integer customerId) {
        LambdaQueryWrapper<Payment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Payment::getCustomerId, customerId)
                .orderByDesc(Payment::getCreateTime);
        return Result.success(baseMapper.selectPage(page, queryWrapper));
    }
}